package com.inspector.app.ui.chat.entity;

import android.graphics.Bitmap;

import com.blankj.utilcode.util.GsonUtils;
import com.inspector.app.ui.chat.utils.DateUtil;
import com.inspector.app.ui.chat.utils.MediaPlayerManager;
import com.inspector.app.ui.chat.utils.VideoThumbnailUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class EntityMessageUtils {


    public static ReceiveMessagesEntity getHistoryToReceive(HistoryMessageEntity historyMessageEntity) {
        ReceiveMessagesEntity receiveMessagesEntity = new ReceiveMessagesEntity();
        receiveMessagesEntity.setMessage(historyMessageEntity.getMessage());//消息
        receiveMessagesEntity.setCreateTime(DateUtil.convertToTimestamp(historyMessageEntity.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));//历史返回时间需要转换成时间戳
        receiveMessagesEntity.setCreateUserId(historyMessageEntity.getCreateUserId());
        receiveMessagesEntity.setMsgDetail(historyMessageEntity.getMsgDetail());
        receiveMessagesEntity.setMsgType(historyMessageEntity.getMsgType());
        receiveMessagesEntity.setSendHeadPic(historyMessageEntity.getSendHeadPic());//头像
        receiveMessagesEntity.setSource(historyMessageEntity.getSource());//人员类型：1：指挥员 2：飞行员 3：观察员
        receiveMessagesEntity.setTaskId(historyMessageEntity.getTaskId());//任务id
        receiveMessagesEntity.setSendUserId(historyMessageEntity.getSendUserId());//发送人id
        receiveMessagesEntity.setSendUserName(historyMessageEntity.getSendUserName());//发送人名字
        receiveMessagesEntity.settShow(historyMessageEntity.gettShow());
        receiveMessagesEntity.setMsgId(historyMessageEntity.getMsgId());
        setAudioTimeAndThumbnail(receiveMessagesEntity);
        return receiveMessagesEntity;
    }

    public static ReceiveMessagesEntity getSendToReceive(SendMessage sendMessage) {
        ReceiveMessagesEntity receiveMessagesEntity = new ReceiveMessagesEntity();
        receiveMessagesEntity.setMessage(sendMessage.getMessage() instanceof String ? (String) sendMessage.getMessage() : GsonUtils.toJson(sendMessage.getMessage()));//消息
        receiveMessagesEntity.setMsgDetail(sendMessage.getMsgDetail());
        receiveMessagesEntity.setMsgType(sendMessage.getMsgType());
        receiveMessagesEntity.setSendHeadPic(sendMessage.getSendHeadPic());//头像
        receiveMessagesEntity.setTaskId(sendMessage.getTaskId());//任务id
        receiveMessagesEntity.setSendUserId(sendMessage.getSendUserId());//发送人id
        receiveMessagesEntity.setSendUserName(sendMessage.getSendUserName());//发送人名字
        Object message = sendMessage.getMessage();
        if (message instanceof Message) {
            Message message1 = (Message) message;
            String content = message1.getContent();
            switch (sendMessage.getMsgDetail()) {
                case 3://音频获取音频时间
                    int duration = MediaPlayerManager.getDuration(content);
                    receiveMessagesEntity.setAudioTime(duration);
                    break;
                case 4:
                    Bitmap firstFrame = VideoThumbnailUtils.getFirstFrame(content);
                    receiveMessagesEntity.setThumbnail(firstFrame);
                    break;
            }
        }
        return receiveMessagesEntity;
    }

    public static SendMessage getReceiveToSend(ReceiveMessagesEntity receiveMessagesEntity) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setMessage(receiveMessagesEntity.getMessage());//消息
        sendMessage.setMsgDetail(receiveMessagesEntity.getMsgDetail());
        sendMessage.setMsgType(receiveMessagesEntity.getMsgType());
        sendMessage.setSendHeadPic(receiveMessagesEntity.getSendHeadPic());//头像
        sendMessage.setTaskId(receiveMessagesEntity.getTaskId());//任务id
        sendMessage.setSendUserId(receiveMessagesEntity.getSendUserId());//发送人id
        sendMessage.setSendUserName(receiveMessagesEntity.getSendUserName());//发送人名字
        return sendMessage;
    }

    public static void setAudioTimeAndThumbnail(ReceiveMessagesEntity receiveMessagesEntity) {
        String message = receiveMessagesEntity.getMessage();
        try {
            JSONObject jsonObject = new JSONObject(message);
            String content = jsonObject.getString("content");
            switch (receiveMessagesEntity.getMsgDetail()) {
                case 3://音频获取音频时间
                    int duration = MediaPlayerManager.getDuration(content);
                    receiveMessagesEntity.setAudioTime(duration);
                    break;
                case 4:
                    Bitmap firstFrame = VideoThumbnailUtils.getFirstFrame(content);
                    receiveMessagesEntity.setThumbnail(firstFrame);
                    break;
            }
        } catch (JSONException e) {

        }

    }

}
